/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package opt;

import dao.Impl.PetDaoImpl;
import dao.Impl.PetStoreDaoImpl;
import dao.PetDao;
import dao.PetStoreDao;
import entity.Pet;
import entity.PetOwner;
import entity.PetStore;
import java.util.List;
import java.util.Scanner;
import opt.Impl.PetOwnerServiceImpl;
import opt.Impl.PetStoreServiceImpl;

/**
 *
 * @author 10592
 */
public class Client {
    static List<Pet> pets;
   
    static Pet pet;
    public static void getStockPet()
    {
        System.out.println("-------以下是库存宠物---------");
        PetDao instance = new PetDaoImpl();
        System.out.println("序号" + "\t"+"宠物编号" + "\t" + "宠物名称" + "\t" + "宠物类型" + "\t" + "宠物价格");
        pets = instance.getAllPets();
        for (int i = 0; i < pets.size(); i++) {
            System.out.println(i+1+"\t"+pets.get(i).getId() + "\t" + pets.get(i).getName() + "\t" + pets.get(i).getKind() + "\t" + pets.get(i).getPrice());
        }
    }
    public static void getOwnerPet(int masterid,String mastertype)
    {
        PetDao instance = new PetDaoImpl();
        System.out.println("-----------我的宠物列表---------");
        System.out.println("序号"+"\t"+"宠物编号" + "\t" + "宠物名称" + "\t" + "宠物类型" + "\t" + "宠物价格" );
        List<Pet> pets = instance.getlist(masterid, mastertype);
        for (int i = 0; i < pets.size(); i++) {
            System.out.print(i+1+ "\t");
            System.out.print(pets.get(i).getId() + "\t");
            System.out.print(pets.get(i).getName() + "\t");
            System.out.print(pets.get(i).getKind() + "\t");
            System.out.print(pets.get(i).getPrice() + "\t");
            System.out.println();
          }
        
    }
    public static Pet getPet(int id,int masterid,String masterkind)
    {
        PetDao instance = new PetDaoImpl();
        Pet pet = instance.getPet(id, masterid, masterkind);
        System.out.println(pet.getId() + "\t" + pet.getName() + "\t" + pet.getKind() + "\t" + pet.getPrice());
        return pet;
    }
    
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String account,password;
        
        int options=0;
        System.out.println("请选择输入登录模式，输入1为宠物主人登录，输入2为宠物商店登录：");
        while (!scanner.hasNext("1")&&!scanner.hasNext("2")) {
            System.out.println("输入错误，请重新输入");
            scanner.next();
        }
        options=scanner.nextInt();
        switch (options) {
            case 1: {
                 PetOwnerService pos=new PetOwnerServiceImpl();
                 PetOwner petowner =pos.login();
                 if (petowner!=null) {
                     System.out.println("登录成功，您可以购买和卖出宠物：");
                     System.out.println("购买宠物：请输入 1");
                     System.out.println("卖出宠物：请输入 2");
                     System.out.println("退出请按任意键");
                     
                    int ways=scanner.nextInt();
                    switch(ways)
                    {
                        case 1:
                       {  
                           getStockPet();
                           System.out.println("请选择要购买的宠物序号");
                           while (!scanner.hasNextInt()) {
                               System.out.println("输入错误，请重新输入");
                               scanner.next();
                           }
                           int num=scanner.nextInt();
                           pet=pets.get(num-1);
                           System.out.println(pet.getId() + "\t" + pet.getName() + "\t" + pet.getKind() + "\t" + pet.getPrice());
                           if (pet==null) {
                               System.out.println("没有该宠物。");
                           }
                           else{
                               pos.buy(pet);
                           }
                           
                           break;}
                        case 2:
                       {
                           getOwnerPet(petowner.getId(), "宠物主人");
                           System.out.println("请选择要卖出的宠物序号");
                           while (!scanner.hasNextInt()) {
                               System.out.println("输入错误，请重新输入");
                               scanner.next();
                           }
                           int num = scanner.nextInt();
                           pet = getPet(num, petowner.getId(), "宠物主人");
                           if (pet!=null) {
                                 pos.sell(pet);
                           } else {
                               System.out.println("没有该宠物，请重新输入");
                           }
                           break;
                        }
                        default:System.out.println("退出登录。。。。");break;
                   }
                }
                 break; }
            case 2:      
                PetStoreService pss=new PetStoreServiceImpl();
                PetStore petStore= pss.login();
                if (petStore!=null) {
                    System.out.println("登录成功，您可以购买和卖出宠物：");
                    System.out.println("购买宠物：请输入 1");
                    System.out.println("卖出宠物：请输入 2");
                    System.out.println("查看本店的宠物：请输入 3");
                    System.out.println("得到账单：请输入 4");
                    System.out.println("退出请按任意键");
                    int ways = scanner.nextInt();
                    switch (ways) {
                        case 1: {
                            getStockPet();
                            System.out.println("请选择要购买的宠物序号");
                            while (!scanner.hasNextInt()) {
                                System.out.println("输入错误，请重新输入");
                                scanner.next();
                            }
                            int num = scanner.nextInt();
                            pet = pets.get(num - 1);
                           
                            if (pet == null) {
                                System.out.println("没有该宠物。");
                            } else {
                                System.out.println(pet.getId() + "\t" + pet.getName() + "\t" + pet.getKind() + "\t" + pet.getPrice());
                                pss.buy(pet);
                            }
                            break;
                        }
                        case 2: {
                            getOwnerPet(petStore.getId(),"店主");
                            System.out.println("请选择要卖出的宠物序号");
                            while (!scanner.hasNextInt()) {
                                System.out.println("输入错误，请重新输入");
                                scanner.next();
                            }
                            int num = scanner.nextInt();
                            pet = getPet(num-1, petStore.getId(),"店主");
                            if (pet != null) {
                                pss.sell(pet);
                            } else {
                                System.out.println("没有该宠物，请重新输入");
                            }
                            break;
                        }
                        case 3:
                        {   
                            pss.getPetInStore(petStore.getId());
                            break;
                        }
                        case 4:
                        {
                            pss.getAccount(petStore.getId());
                           break;
                        }
                        default:
                            System.out.println("退出登录。。。。");
                            break;
                    }
                }
                break; }
              }  
                       
        
   
}
